// |**| Opcon|OperationMango|**|Opcon|OperationMango|**|
// |**| Source Code for the FPS Operation Mango     |**|
// |**| Opcon|OperationMango|**|Opcon|OperationMango|**|

using System;
using OpenTK.Graphics;
using OpenTK;
using OpenTK.Graphics.OpenGL;

namespace OperationMango
{

	public class CameraBase : IDynamic
	{
		
		protected Matrix4 m_RotationMatrix;
		protected Matrix4 m_TranslationMatrix;
		protected Matrix4 m_CameraMatrix;
		protected Matrix4 m_PreviousCameraMatrix;
		protected Matrix4 m_TemporaryMatrix;
		protected Quaternion m_Rotation;
		protected Quaternion m_RotationX;
		protected Quaternion m_RotationY;
		protected Quaternion m_RotationN;
		protected Quaternion m_RotationXN;
		protected Quaternion m_RotationYN;
		protected Vector3 m_Position;

		public CameraBase ()
		{
			
		}
		
		public void Update(double time)
		{
			
		}
		
		protected void RotateCamera (ref Quaternion rotate)
		{
			
			m_RotationMatrix = Matrix4.Rotate (rotate);
			
		}
		
		protected void RotateCameraX (float angle)
		{
			m_RotationX = Quaternion.FromAxisAngle(Vector3.UnitX, angle);
		}
		
		
		
		protected void RotateCameraY (float angle)
		{
			m_RotationY = Quaternion.FromAxisAngle(Vector3.UnitY, angle);
		}
		
		protected void CombineRotations ()
		{
			Quaternion.Multiply(ref m_RotationY, ref m_RotationX, out m_Rotation);
			//Quaternion.Normalize(ref m_RotationX, out m_RotationXN);
			//Quaternion.Normalize(ref m_RotationY, out m_RotationYN);
		}
		
		protected void TranslateCamera (Vector3 position)
		{
			Matrix4.CreateTranslation (ref position, out m_TranslationMatrix);
		}
		
		protected void FinalizeMatrices ()
		{
			//Matrix4.Mult (ref m_TranslationMatrix, ref m_RotationMatrix, out m_TemporaryMatrix);
			//Matrix4.Mult(ref m_PreviousCameraMatrix, ref m_TemporaryMatrix, out m_CameraMatrix);
			m_CameraMatrix *= Matrix4.Rotate(m_Rotation);
			m_PreviousCameraMatrix = m_CameraMatrix;
		}
		
		protected void UpdateMatrices()
		{
			
			GL.MatrixMode(MatrixMode.Modelview);
			GL.LoadMatrix(ref m_CameraMatrix);
			Console.WriteLine ("m_CameraMatrix={0}", m_CameraMatrix);
		}
		
		public void Test ()
		{
			throw new System.NotImplementedException ();
		}
	}
}
